#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : xuexiaohua
# @time    : 2025/8/19 19:15
# @function: the script is used to do something.
# @version :
import requests
from jsonpath import jsonpath

from wework.apis.wework import WeWork

'''
完成业务接口描述
'''
class Departments(WeWork):

    def create(self, data):
        '''
        创建部门接口
        :return:
        '''
        create_url = f"{self.base_url}/cgi-bin/department/create?access_token={self.token}"
        # 方法一
        r = requests.post(create_url, json=data)
        # 方法二
        req = {
            "method": "POST",
            "url": create_url,
            "json": data
        }
        r = self.send_api(req)
        return r


    def update(self,data):
        '''
        更新部门接口
        :return:
        '''
        update_url = f"{self.base_url}/cgi-bin/department/update?access_token={self.token}"
        # 方法一
        # r = requests.post(update_url, json=data)
        # 方法二
        req = {
            "method": "POST",
            "url": update_url,
            "json": data
        }
        r = self.send_api(req)
        return r

    def get(self, depart_id=None):
        '''
        获取子部门id列表接口
        :return:
        '''
        get_url = f"{self.base_url}/cgi-bin/department/simplelist?access_token={self.token}"
        if depart_id is not None:
            get_url += f"&id={depart_id}"
        # 方法一
        # r = requests.get(get_url)
        # 方法二
        req = {
            "method": "GET",
            "url": get_url
        }
        r = self.send_api(req)
        return r.json()

    def delete(self, depart_id):
        '''
        删除部门接口
        :return:
        '''
        delete_url = f"{self.base_url}/cgi-bin/department/delete?access_token={self.token}&id={depart_id}"
        # 方法一
        # r = requests.get(delete_url)
        # 方法二
        req = {
            "method": "GET",
            "url": delete_url
        }
        r = self.send_api(req)
        return r.json()

    def clear(self):
        '''
        清理除去夫部门之外，已经存在的部门
        :return:
        '''
        # 获取所有部门的id
        r = self.get()
        # 提取所有部门的id
        ids = jsonpath(r.json(), '$..id')
        for id in ids:
            if id != 1:
                # 调用删除部门接口
                self.delete(id)
